package com.group.itms.routes.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.group.itms.routes.R;
import com.group.itms.routes.data.BObject;

public class LocationDetailActivity extends BaseActivity {
	
	private static final String TAG = "LocationDetailActivity";
	
	private BObject mLocation;
	
	public static Intent getIntent(Context c, int locationId) {
		Intent intent = new Intent(c, LocationDetailActivity.class);
		intent.putExtra(OBJECT_ID, locationId);
		return intent;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		ListView navigation = new ListView(this);
		navigation.setAdapter(new NavigationAdapter(this));
		navigation.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				NavigationItem navItem = (NavigationItem) view.getTag();
				startActivity(navItem.intent, true);
			}
		});
		setContentView(navigation);
	}

	private void init() {
		Bundle args = getIntent().getExtras();
		if (args == null) {
			throw new RuntimeException("LocationDetailActivity does not have aruments");
		}
		int locationId = args.getInt(OBJECT_ID, -1);
		if (locationId == -1) {
			throw new RuntimeException("Missing args with key: " + OBJECT_ID + " to activity: " + getClass());
		}
		mLocation = getObject(locationId);
		if (mLocation == null) {
			throw new RuntimeException("Unable to find Location widh id: " + locationId);
		}
		setTitle(mLocation.getName());
	}

	@Override
	protected String getTag() {
		return TAG;
	}
	
	private class NavigationAdapter extends ArrayAdapter<Void> {

		private static final int NAVIGATION_ITEM_COUNT = 6;
		
		private static final int NAVIGATION_TEIM_TOPICS = 0;
		private static final int NAVIGATION_TEIM_INFO = 1;
		private static final int NAVIGATION_TEIM_ROUTES = 2;
		private static final int NAVIGATION_TEIM_POINTS = 3;
		private static final int NAVIGATION_TEIM_TASKS = 4;
		private static final int NAVIGATION_TEIM_MAP = 5;
		
		public NavigationAdapter(Context context) {
			super(context, 0);
		}
		
		@Override
		public int getCount() {
			return NAVIGATION_ITEM_COUNT;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			NavigationItem navItem;
			if (convertView == null) {
				convertView = inflate(R.layout.navigation_item);
				navItem = new NavigationItem(convertView);
				convertView.setTag(navItem);
			} else {
				navItem = (NavigationItem) convertView.getTag();
			}
			int locId = mLocation.getId();
			switch (position) {
			case NAVIGATION_TEIM_TOPICS:
				navItem.setIcon(R.drawable.ic_topic);
				navItem.setLabel("Topics");
				navItem.intent = TopicsActivity.getIntent(getContext(), locId);
				break;
			case NAVIGATION_TEIM_INFO:
				navItem.setIcon(R.drawable.ic_info);
				navItem.setLabel("Info");
				navItem.intent = LocationInfoActivity.getIntent(getContext(), locId);
				break;
			case NAVIGATION_TEIM_ROUTES:
				navItem.setIcon(R.drawable.ic_route);
				navItem.setLabel("Routes");
				navItem.intent = RouteListActivity.getIntent(getContext(), locId);
				break;
			case NAVIGATION_TEIM_POINTS:
				navItem.setIcon(R.drawable.ic_point);
				navItem.setLabel("Points");
				navItem.intent = InformationPointListActivity.getIntent(getContext(), locId);
				break;
			case NAVIGATION_TEIM_TASKS:
				navItem.setIcon(R.drawable.ic_task);
				navItem.setLabel("Tasks");
				navItem.intent = TasksActivity.getIntent(getContext(), locId);
				break;
			case NAVIGATION_TEIM_MAP:
				navItem.setIcon(R.drawable.ic_map);
				navItem.setLabel("Map");
				navItem.intent = NewMapActivity.getIntent(getContext(), locId);
				break;
			}
			
			return convertView;
		}
		
		private View inflate(int resId) {
			return getLayoutInflater().inflate(resId, null);
		}
		
		
	}
	
	private static class NavigationItem {
		private ImageView icon;
		private TextView label;
		private Intent intent;
		
		public NavigationItem(View v) {
			icon = (ImageView) v.findViewById(R.id.icon);
			label = (TextView) v.findViewById(R.id.label);
		}
		
		public void setIcon(int resId) {
			icon.setImageResource(resId);
		}
		
		public void setLabel(String text) {
			label.setText(text);
		}
	}
	
}
